package com.hrt.datasender.datadao.service;

import java.util.List;

import com.btcode.db.result.Row;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.model.BcSendPlatformParamModel;
import com.hrt.db.DBManager;

public final class BcSendPlatformParamService {

    private static volatile BcSendPlatformParamService instance;

    private BcSendPlatformParamService() {

    }

    public static BcSendPlatformParamService getInstance() {
        if (instance == null) {
            synchronized (BcSendPlatformParamService.class) {
                if (instance == null) {
                    instance = new BcSendPlatformParamService();
                }
            }
        }
        return instance;
    }

    public List<BcSendPlatformParamModel> getPlatformParam(String platformId) {
        String sql = "SELECT * FROM BC_SEND_PLATFORM_PARAM WHERE PLATFORM_ID = '" + platformId + "'";
        try {
            return DBManager.getORMExecuter().getModelListSQL(sql, null, BcSendPlatformParamModel.class);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Row> getPlatformParamForReport(String platformId) {
        String sql = "select t1.* from BC_SEND_PLATFORM_PARAM t "
                + "left join bc_monitor_param_dict t1 on t.param_code_bus = t1.param_code_bus"
                + " where platform_id = '" + platformId + "'";
        try {

            List<Row> result = DBManager.getDataExecuter().queryList(sql, null);
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean save(BcSendPlatformParamModel model) {
        BcSendPlatformParamModel vmodel = null;
        try {
            if (model.getPlatformId() != null) {
                vmodel = DBManager.getORMExecuter().getModel(model);
            }
            if (vmodel == null) {
                DBManager.getORMExecuter().create(model);
            }
            else {
                DBManager.getORMExecuter().update(model);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean delete(String platformId) {
        String sql = "DELETE FROM BC_SEND_PLATFORM_PARAM WHERE PLATFORM_ID = '" + platformId + "'";
        try {
            DBManager.getDataExecuter().delete(sql, null);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public String[] getPlatformParam(String ip, String port) {

        BcSendPlatformModel sendPlatform = BcSendPlatformService.getInstance().getPlatformInfo(ip, port);
        String platFormid = sendPlatform.getPlatformId();

        String sql = "SELECT * FROM BC_SEND_PLATFORM_PARAM WHERE PLATFORM_ID = '" + platFormid + "'";

        List<BcSendPlatformParamModel> list = DBManager.getORMExecuter().getModelListSQL(sql, null,
                BcSendPlatformParamModel.class);

        if (list.size() < 1) {
            return null;
        }

        String[] params = new String[list.size()];

        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i).getParamCodeBus();
        }
        return params;
    }
}